import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import 'home/index.dart';
import 'order/index.dart';
import 'my/index.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import './components/loading.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    Loading.init();
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        splashFactory: NoSplash.splashFactory,
      ),
      home: const MyHomePage(),
      builder: EasyLoading.init(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  late TabController _tabController;
  int _previousIndex = 0;
  
  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 3, vsync: this);
    _tabController.addListener(() {
      setState(() {
        _previousIndex = _tabController.previousIndex;
      });
    });
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 设置为 false 去掉顶部安全区域的 padding
      extendBodyBehindAppBar: true,
      // 设置为透明 AppBar
      
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        toolbarHeight: 0, // 设置高度为0来隐藏
      ),
      body: GFTabBarView(
        controller: _tabController,
        physics: const NeverScrollableScrollPhysics(),
        children: const <Widget>[
          Center(child: HomePage()),
          Center(child: OrderPage()),
          Center(child: MyPage()),
        ],
      ),
      bottomNavigationBar: Container(
        decoration: BoxDecoration(
          borderRadius: const BorderRadius.only(
            topLeft: Radius.circular(10),
            topRight: Radius.circular(10),
          ),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.1),
              blurRadius: 10,
              spreadRadius: 2,
              offset: const Offset(0, -2),
            ),
          ],
        ),
        child: ClipRRect(
          borderRadius: const BorderRadius.only(
            topLeft: Radius.circular(10),
            topRight: Radius.circular(10),
          ),
          child: GFTabBar(
            controller: _tabController,
            length: 3,
            indicatorColor: Colors.transparent,
            indicatorWeight: 3.0,
            labelColor: Colors.black,
            unselectedLabelColor: Colors.grey,
            tabBarColor: Colors.white,
            tabs: <Widget>[
              Tab(
                icon: _buildAnimatedIcon(
                  currentIndex: _tabController.index,
                  targetIndex: 0,
                  previousIndex: _previousIndex,
                  activeIcon: 'assets/images/tabbar/home_active.png',
                  inactiveIcon: 'assets/images/tabbar/home.png',
                ),
                text: '首页'
              ),
              Tab(
                icon: _buildAnimatedIcon(
                  currentIndex: _tabController.index,
                  targetIndex: 1,
                  previousIndex: _previousIndex,
                  activeIcon: 'assets/images/tabbar/order_active.png',
                  inactiveIcon: 'assets/images/tabbar/order.png',
                ),
                text: '收益'
              ),
              Tab(
                icon: _buildAnimatedIcon(
                  currentIndex: _tabController.index,
                  targetIndex: 2,
                  previousIndex: _previousIndex,
                  activeIcon: 'assets/images/tabbar/my_active.png',
                  inactiveIcon: 'assets/images/tabbar/my.png',
                ),
                text: '我的'
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildAnimatedIcon({
    required int currentIndex,
    required int targetIndex,
    required int previousIndex,
    required String activeIcon,
    required String inactiveIcon,
  }) {
    final bool shouldAnimate = (currentIndex == targetIndex && previousIndex == targetIndex - 1) ||
                              (currentIndex == targetIndex && previousIndex == targetIndex + 1) ||
                              (previousIndex == targetIndex && currentIndex == targetIndex - 1) ||
                              (previousIndex == targetIndex && currentIndex == targetIndex + 1);

    return AnimatedSwitcher(
      duration: const Duration(milliseconds: 300),
      transitionBuilder: (Widget child, Animation<double> animation) {
        return shouldAnimate ? ScaleTransition(scale: animation, child: child) : child;
      },
      child: Image.asset(
        currentIndex == targetIndex ? activeIcon : inactiveIcon,
        key: ValueKey<int>(currentIndex == targetIndex ? 1 : 0),
        width: 24,
        height: 24,
      ),
    );
  }
}
